import { _decorator, Button, Camera, Component, Event, game, instantiate, Label, Node, screen, view } from 'cc';
import { Game } from './Game';
const { ccclass, property } = _decorator;

@ccclass('GameUI')
export class GameUI extends Component {
    @property(Camera)
    public uiCamera: Camera = null

    @property(Label)
    public labelPlayerLv: Label = null

    @property(Label)
    public labelSpawnInterval: Label = null

    @property(Label)
    public labelMonsters: Label = null

    @property(Node)
    public labelMonsterTemplate: Node = null

    @property(Label)
    public labelSpecialCountDown: Label = null

    @property(Node)
    public nodeSetting: Node = null

    public _game: Game = null

    onLoad() {
        this._game = this.node.parent.getChildByName('Game').getComponent(Game)
    }

    start() {
        const size = screen.windowSize;
        this.uiCamera.orthoHeight = size.height / view.getScaleY() / 2;
    }

    update() {
        const playerMaxLv = this._game.getPlayerMaxLv()
        this.labelPlayerLv.string = '角色当前最大等级:' + playerMaxLv.toString()

        const parent = this.labelMonsters.node
        const children = parent.children
        const infos = this._game.getMonsterInfos()
        const cfg = infos.config[playerMaxLv - 1]
        const keys = Object.keys(infos.nums)
        keys.forEach((lv, index) => {
            let child = children[index]
            if (!child) {
                child = instantiate(this.labelMonsterTemplate)
                child.setPosition(20, -50 * (index + 1))
                child.parent = parent
            }
            child.getComponent(Label).string = '等级:' + lv + ' 数量:' + infos.nums[lv] + ' 设定数量:' + cfg[Number(lv) - 1]
            child.getComponent(Button).clickEvents[0].customEventData = playerMaxLv + '_' + lv
        })

        for (let i = children.length - 1; i >= keys.length; i--) {
            children[i].destroy()
        }

        const spawnInfo = this._game.getSpawnInfo()
        this.labelSpawnInterval.string = ' 刷新倒计时:' + Math.floor(spawnInfo.spawnTime / 1000) + ' 刷新间隔:' + spawnInfo.interval

        const countdown = this._game.getSpecialTime() ? (this._game.getSpecialTime() - Date.now()) / 1000 : undefined
        this.labelSpecialCountDown.string = countdown ? Math.floor(countdown).toString() : ''
    }

    onBtnResume() {
        this._game.gameResume()
        this.nodeSetting.active = false
    }

    onBtnShowLvSetting(evt: Event, customData: string) {
        this._game.gamePause()
        this.nodeSetting.active = true
        const btnAdd = this.nodeSetting.getChildByName('Btn_Add').getComponent(Button)
        const btnMinus = this.nodeSetting.getChildByName('Btn_Minus').getComponent(Button)
        btnAdd.clickEvents[0].customEventData = customData
        btnMinus.clickEvents[0].customEventData = customData
    }


    onBtnAddConfig(evt: Event, customData: string) {
        // const btn = evt.target.node as Node
        const [playerLv, monsterLv] = customData.split('_')
        let storage = localStorage.getItem('Config')
        if (storage) {
            storage = JSON.parse(storage)
            storage[playerLv] = storage[playerLv] || {}
            storage[playerLv][monsterLv] = (storage[playerLv][monsterLv] || 0) + 1
        }
        else {
            storage = {[playerLv]: {[monsterLv]: 1}}
        }
        localStorage.setItem('Config', JSON.stringify(storage))
    }

    onBtnMinusConfig(evt: Event, customData: string) {
        const [playerLv, monsterLv] = customData.split('_')
        let storage = localStorage.getItem('Config')
        if (storage) {
            storage = JSON.parse(storage)
            storage[playerLv] = storage[playerLv] || {}
            storage[playerLv][monsterLv] = (storage[playerLv][monsterLv] || 0) - 1
        }
        else {
            storage = {[playerLv]: {[monsterLv]: -1}}
        }
        localStorage.setItem('Config', JSON.stringify(storage))
    }
    
    onBtnRestart() {
        game.restart()
    }

    onToggleView() {
        this._game.toggleView()
    }

    onBtnEvolution() {
        this._game.evolutionPlayer()
    }

    onBtnSpecial() {
        this._game.startSpecial()
    }

    onBtnFlyForward() {
        this._game.flyForward()
    }

    onBtnSpawnFollowMonster() {
        this._game.spawnFollowMonster()
    }
}


